package com.program.beautymakeup.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.hutool.core.collection.ListUtil;
import com.program.beautymakeup.controller.form.ConfigurationForm;
import com.program.beautymakeup.controller.form.PortfolioTypeForm;
import com.program.beautymakeup.domain.entity.Configuration;
import com.program.beautymakeup.domain.entity.PortfolioType;
import com.program.beautymakeup.domain.vo.ResultInfo;
import com.program.beautymakeup.mapper.ConfigurationMapper;
import com.program.beautymakeup.mapper.PortfolioTypeMapper;
import com.program.beautymakeup.service.UploadService;
import com.program.beautymakeup.utils.ResultInfoUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author CaiKe
 * @date 2023/2/10 18:54
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/config")
public class ConfigController {

    private final ConfigurationMapper configMapper;
    private final UploadService uploadService;
    private final PortfolioTypeMapper typeMapper;

    @GetMapping("/list")
    @SaCheckRole("1")
    public ResultInfo<List<Configuration>> listConfig(){
        final List<Configuration> configs = configMapper.selectList(null);
        return ResultInfoUtil.buildSuccess(configs);
    }

    @PostMapping("/changeConfig")
    @SaCheckRole("1")
    public ResultInfo changeConfig(@RequestBody ConfigurationForm form) {
        Configuration configuration = Configuration.builder()
                .comment(form.getComment())
                .value(form.getValue())
                .name(form.getName()).build();
        Integer id = form.getId();
        // 如果id不为空那么说明是修改
        if (form.getId() != null) {
            configuration.setId(id);
            configMapper.updateByPrimaryKeySelective(configuration);
        }
        // 如果没有id说明是新增
        else {
            configMapper.insertSelective(configuration);
            id = configuration.getId();
        }
        return ResultInfoUtil.buildSuccess(configMapper.selectByPrimaryKey(id));
    }

    @PutMapping("/list/{id}")
    public ResultInfo<Void> updateConfig(@PathVariable("id")Integer id, MultipartFile img, HttpServletRequest request){
        String url = null;
        if(img!=null){
            url = uploadService.uploadImg(ListUtil.of(img));
        }
        final Configuration configToUpdate = configMapper.selectByPrimaryKey(id).withValue(url);
        configMapper.updateByPrimaryKey(configToUpdate);
        return ResultInfoUtil.buildSuccess();
    }

    @GetMapping("/portfolio_type")
    public ResultInfo<List<PortfolioType>> listType(){
        final List<PortfolioType> portfolioTypes = typeMapper.selectAll();
        return ResultInfoUtil.buildSuccess(portfolioTypes);
    }

    @PostMapping("/portfolio_type")
    public ResultInfo<List<PortfolioType>> addNewType(@RequestBody PortfolioTypeForm type){
        typeMapper.insertSelective(PortfolioType.builder().name(type.getName()).build());
        return ResultInfoUtil.buildSuccess();
    }

    @DeleteMapping("/portfolio_type/{id}")
    public ResultInfo<List<PortfolioType>> addNewType(@PathVariable("id")Integer id){
        typeMapper.deleteById(id);
        return ResultInfoUtil.buildSuccess();
    }








}